﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections;
using System.Reflection;
using System.IO;
using System.Xml;
using System.Diagnostics;
using TomanuExtensions;

namespace AmiEmuLib.Configuration.FloppySoundConfig
{
    /// <summary>
    /// Sounds recorded from diffrent floppies.
    /// </summary>
    [DebuggerDisplay("Count = {Count}")]
    public static class FloppySoundsList
    {
        /// <summary>
        /// List of sounds.
        /// </summary>
        private static List<FloppySounds> m_list = new List<FloppySounds>();

        /// <summary>
        /// Constructor.
        /// </summary>
        static FloppySoundsList()
        {
            using (var stream = StreamExtensions.FromResource(typeof(FloppySoundsList), 
                "FloppySoundsList.xml"))
            {
                XmlReaderExtensions.ReadXml(stream, (reader) =>
                {
                    reader.ReadStartElement(typeof(FloppySoundsList).Name);

                    while (reader.IsStartElement())
                        m_list.Add(new FloppySounds(reader));

                    reader.ReadEndElement();
                });
            }
        }

        /// <summary>
        /// Save data to xml. Only for development.
        /// </summary>
        public static void SaveToXml()
        {
            XmlWriterExtensions.WriteXml("FloppySoundsList.xml", (writer) =>
            {
                writer.WriteStartElement(typeof(FloppySoundsList).Name);

                foreach (var fsnd in m_list)
                    fsnd.WriteXml(writer);

                writer.WriteEndElement();
            });
        }

        /// <summary>
        /// Sounds from name.
        /// </summary>
        /// <param name="a_name"></param>
        /// <returns></returns>
        public static FloppySounds GetSounds(string a_name)
        {
            foreach (var fs in m_list)
                if (fs.Name == a_name)
                    return fs;
            return null;
        }

        /// <summary>
        /// List of sounds.
        /// </summary>
        /// <returns></returns>
        public static IEnumerable<FloppySounds> List
        {
            get
            {
                return m_list;
            }
        }
    }
}
